package com.xnyzc.lhy.mis.service.impl.message;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.push.EJPushConfigKey;
import com.xnyzc.lhy.common.penum.push.EPushCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.message.OaSysMessagePush;
import com.xnyzc.lhy.mis.entity.param.message.OaSysMessagePushParam;
import com.xnyzc.lhy.mis.entity.user.OaDUser;
import com.xnyzc.lhy.mis.feign.FeignMessageSendService;
import com.xnyzc.lhy.mis.mapper.message.OaSysMessagePushMapper;
import com.xnyzc.lhy.mis.mapper.user.OaDUserMapper;
import com.xnyzc.lhy.mis.service.message.IOaSysMessagePushService;
import com.xnyzc.lhy.resource.entity.jpush.BizResult;
import com.xnyzc.lhy.resource.entity.jpush.PushInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 后台管理 Push消息表 服务实现类
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-11-09
 */
@Service
public class OaSysMessagePushServiceImpl extends ServiceImpl<OaSysMessagePushMapper, OaSysMessagePush> implements IOaSysMessagePushService {

    @Autowired
    private OaSysMessagePushMapper oaSysMessagePushMapper;

    @Autowired
    private OaDUserMapper oaDUserMapper;

    @Autowired
    private FeignMessageSendService feignMessageSendService;


    @Override
    public Rv addMessagePushInfo(OaSysMessagePushParam oaSysMessagePushParam) {
        OaSysMessagePush oaSysMessagePush = new OaSysMessagePush();
        oaSysMessagePush.setCreateTime(new Date());
        oaSysMessagePush.setCreateUser(1L);
        oaSysMessagePush.setPushId(IDUtil.nextId());
        oaSysMessagePush.setPushBody(oaSysMessagePushParam.getPushBody());
        oaSysMessagePush.setPushLink(oaSysMessagePushParam.getPushLink());
        oaSysMessagePush.setPushTitle(oaSysMessagePushParam.getPushTitle());
        oaSysMessagePushMapper.insert(oaSysMessagePush);
        QueryWrapper<OaDUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.select(OaDUser.DRIVER_PHONE);
        List<OaDUser> dUserList=oaDUserMapper.selectList(queryWrapper);
        List<String> phones = new ArrayList<>();
        if(!dUserList.isEmpty()){
            for(OaDUser user:dUserList){
                phones.add(user.getDriverPhone());
            }
            List<String> aliasList=new ArrayList<>();
            int i = 0;
            // 分批推送，极光推送每次可推1000条
            for (i = 0; i < phones.size(); i++) {
                aliasList.add(phones.get(i));
                if (aliasList.size() % 1000 == 0 && aliasList.size() != 0) {
                    // 推送消息
                    this.driverPush(aliasList, oaSysMessagePushParam.getPushTitle(), oaSysMessagePushParam.getPushBody(),oaSysMessagePushParam.getPushLink());
                    aliasList.clear();
                }
            }
            this.driverPush(aliasList, oaSysMessagePushParam.getPushTitle(), oaSysMessagePushParam.getPushBody(),oaSysMessagePushParam.getPushLink());
        }
        return Rv.wrap(EErrorCode.success);
    }

    /**
     * 给司机推送
     */
    private void driverPush(List<String> phones, String title, String content,String pushLink){
        if(CheckUtil.objIsEmpty(phones)){
            return;
        }
        JSONObject jsonObject = new JSONObject();
        JSONObject result = new JSONObject();
        result.put("title", title);
        jsonObject.put("expand", JSON.toJSONString(result));
        PushInfoEntity pushInfoEntity = new PushInfoEntity();
        pushInfoEntity.setPushCode(EPushCode.fullPush.getValue());
        pushInfoEntity.setPhones(phones);
        pushInfoEntity.setExtras(jsonObject);
        pushInfoEntity.setContent(content);
        pushInfoEntity.setTag("lhy");
        pushInfoEntity.setClientId(EJPushConfigKey.driver.getValue());
        pushInfoEntity.setJudgeCms("CMS");
        pushInfoEntity.setTitle(title);
        pushInfoEntity.setPushLink(pushLink);
        feignMessageSendService.pushMessage(pushInfoEntity);
    }

    @Override
    public Rv queryMessagePushInfo(Long current,Long pageSize) {

        IPage iPage = new Page();
        iPage.setSize(pageSize);
        iPage.setCurrent(current);
//        QueryWrapper<OaSysMessagePush> queryWrapper = new QueryWrapper<>();
//        IPage oaSysMessagePushes  = oaSysMessagePushMapper.selectPage(iPage, queryWrapper);
        List<OaSysMessagePush> records  = oaSysMessagePushMapper.querySysMessagePushList(iPage);
        iPage.setRecords(records);
        return Rv.wrap(EErrorCode.success,iPage);
    }
}
